{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "771a12a6-8a9b-4216-96ab-cc029f6f7a8d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:47:24.437274Z",
     "iopub.status.busy": "2022-02-06T08:47:24.436743Z",
     "iopub.status.idle": "2022-02-06T08:47:24.531949Z",
     "shell.execute_reply": "2022-02-06T08:47:24.531343Z",
     "shell.execute_reply.started": "2022-02-06T08:47:24.437199Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79eb52f5-fb4c-4bc3-b5d5-8934380cc1ec",
   "metadata": {},
   "source": [
    "1. 数组可以和数字直接进行运算\n",
    "2. 两个shape相同的数组可以进行运算\n",
    "3. 两个shape不同的数组要计算则需要满足广播机制\n",
    "    1. 两个数组从末尾算起的维度的轴长度相同或者其中一方的长度为1，则认为它们是广播兼容的。\n",
    "    2. 广播会在缺失和长度为1的的维度上进行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c3898f02-f7fd-44db-92c1-f798856c06c6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:47:26.967295Z",
     "iopub.status.busy": "2022-02-06T08:47:26.967044Z",
     "iopub.status.idle": "2022-02-06T08:47:26.972433Z",
     "shell.execute_reply": "2022-02-06T08:47:26.971742Z",
     "shell.execute_reply.started": "2022-02-06T08:47:26.967270Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[1 3 3 0 1 0]\n",
      " [2 0 2 0 1 3]\n",
      " [3 0 3 2 1 0]\n",
      " [3 2 4 2 3 1]]\n",
      "====================\n",
      "a1 * 2: \n",
      "[[2 6 6 0 2 0]\n",
      " [4 0 4 0 2 6]\n",
      " [6 0 6 4 2 0]\n",
      " [6 4 8 4 6 2]]\n"
     ]
    }
   ],
   "source": [
    "# 数组与数的运算\n",
    "a1 = np.random.randint(0, 5, size=(4, 6))  # 随机生成4行6列的数组a1，其中元素为0到4直接的整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1 * 2: \\n{}\".format(a1 * 2))  # 让a1中所有元素的值都翻一倍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1da02bb0-73d9-4cf8-99b5-72b52682351f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:48:39.418050Z",
     "iopub.status.busy": "2022-02-06T08:48:39.417806Z",
     "iopub.status.idle": "2022-02-06T08:48:39.424980Z",
     "shell.execute_reply": "2022-02-06T08:48:39.424131Z",
     "shell.execute_reply.started": "2022-02-06T08:48:39.418025Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a2:\n",
      "[[2 2 1 4 4 3]\n",
      " [0 3 3 4 0 3]\n",
      " [1 1 0 0 4 4]\n",
      " [2 0 1 3 0 0]]\n",
      "====================\n",
      "a1:\n",
      "[[1 3 3 0 1 0]\n",
      " [2 0 2 0 1 3]\n",
      " [3 0 3 2 1 0]\n",
      " [3 2 4 2 3 1]]\n",
      "====================\n",
      "a1 + a2:\n",
      "[[3 5 4 4 5 3]\n",
      " [2 3 5 4 1 6]\n",
      " [4 1 3 2 5 4]\n",
      " [5 2 5 5 3 1]]\n"
     ]
    }
   ],
   "source": [
    "# 数组与数组的运算\n",
    "# shape相同的数组之间的运算\n",
    "a2 = np.random.randint(0, 5, size=(4, 6))  # 随机生成4行6列的数组a2，其中元素为0到4直接的整数\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1 + a2:\\n{}\".format(a1 + a2))  # 采用广播机制，让a1与a2对应位置相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9fbbad99-2ea1-431d-86ea-4b2ebd44e2ae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:49:39.444476Z",
     "iopub.status.busy": "2022-02-06T08:49:39.444232Z",
     "iopub.status.idle": "2022-02-06T08:49:39.899089Z",
     "shell.execute_reply": "2022-02-06T08:49:39.898329Z",
     "shell.execute_reply.started": "2022-02-06T08:49:39.444451Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a3:\n",
      "[[2 1 4 4 1]\n",
      " [3 2 1 4 1]\n",
      " [1 4 2 0 3]\n",
      " [3 2 3 1 3]]\n",
      "====================\n",
      "a2:\n",
      "[[2 2 1 4 4 3]\n",
      " [0 3 3 4 0 3]\n",
      " [1 1 0 0 4 4]\n",
      " [2 0 1 3 0 0]]\n",
      "====================\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (4,5) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma2:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(a2))\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m=\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m20\u001b[39m)\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma2 + a3:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\u001b[43ma2\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43ma3\u001b[49m))\n",
      "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,6) (4,5) "
     ]
    }
   ],
   "source": [
    "# shape不同的数组之间的运算\n",
    "a3 = np.random.randint(0, 5, size=(4, 5))  # 随机生成4行5列的数组a3，其中元素为0到4直接的整数\n",
    "print(\"a3:\\n{}\".format(a3))\n",
    "print(\"=\" * 20)\n",
    "print(\"a2:\\n{}\".format(a2))\n",
    "print(\"=\" * 20)\n",
    "print(\"a2 + a3:\\n{}\".format(a2 + a3))  # 两个结构不同个数组不能运用广播机制进行运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7837ffbf-2731-454a-b191-673b118929d1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:50:20.631057Z",
     "iopub.status.busy": "2022-02-06T08:50:20.630809Z",
     "iopub.status.idle": "2022-02-06T08:50:20.637515Z",
     "shell.execute_reply": "2022-02-06T08:50:20.636811Z",
     "shell.execute_reply.started": "2022-02-06T08:50:20.631033Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[1 3 3 0 1 0]\n",
      " [2 0 2 0 1 3]\n",
      " [3 0 3 2 1 0]\n",
      " [3 2 4 2 3 1]]\n",
      "====================\n",
      "a4:\n",
      "[[2]\n",
      " [0]\n",
      " [4]\n",
      " [2]]\n",
      "====================\n",
      "a1 + a4:\n",
      "[[3 5 5 2 3 2]\n",
      " [2 0 2 0 1 3]\n",
      " [7 4 7 6 5 4]\n",
      " [5 4 6 4 5 3]]\n"
     ]
    }
   ],
   "source": [
    "a4 = np.random.randint(0, 5, size=(4, 1))  # 随机生成4行1列的数组a4，其中元素为0到4直接的整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a4:\\n{}\".format(a4))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1 + a4:\\n{}\".format(a1 + a4))  # 采用广播机制，让a4与a1中每一列对应位置相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "85d73966-dfa2-4b80-95a8-cf9c041fe608",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-06T08:50:43.389461Z",
     "iopub.status.busy": "2022-02-06T08:50:43.389209Z",
     "iopub.status.idle": "2022-02-06T08:50:43.395688Z",
     "shell.execute_reply": "2022-02-06T08:50:43.395052Z",
     "shell.execute_reply.started": "2022-02-06T08:50:43.389435Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a1:\n",
      "[[1 3 3 0 1 0]\n",
      " [2 0 2 0 1 3]\n",
      " [3 0 3 2 1 0]\n",
      " [3 2 4 2 3 1]]\n",
      "====================\n",
      "a5:\n",
      "[[3 0 0 2 4 2]]\n",
      "====================\n",
      "a1 + a5:\n",
      "[[4 3 3 2 5 2]\n",
      " [5 0 2 2 5 5]\n",
      " [6 0 3 4 5 2]\n",
      " [6 2 4 4 7 3]]\n"
     ]
    }
   ],
   "source": [
    "a5 = np.random.randint(0, 5, size=(1, 6))  # 随机生成1行6列的数组a5，其中元素为0到4直接的整数\n",
    "print(\"a1:\\n{}\".format(a1))\n",
    "print(\"=\" * 20)\n",
    "print(\"a5:\\n{}\".format(a5))\n",
    "print(\"=\" * 20)\n",
    "print(\"a1 + a5:\\n{}\".format(a1 + a5))  # 采用广播机制，让a5与a1中每一行对应位置相加"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
